import os
import yaml
from lib.logger import logger
from ui import Win as MainWin
from control import Controller as MainUIController
import ttkbootstrap
from parse_license import LicenseCheck
import tkinter as tk
CONFIG_PATH = ''


def init_service():
    global CONFIG_PATH
    if CONFIG_PATH == '':
        CONFIG_PATH = os.getenv("FILE", 'config/config.yaml')
    with open(CONFIG_PATH, 'r') as f:
        config = yaml.safe_load(f)
        logger.setup(config)


def test_time(over_time):
    from datetime import datetime
    d2 = datetime.strptime(over_time, '%Y-%m-%d %H:%M:%S')
    now = datetime.now()
    if d2 > now:
        return True
    else:
        return False


if __name__ == '__main__':
    # 该方法作用是阻止子进程运行其后面的代码
    license_check_obj = LicenseCheck()
    res_data = license_check_obj.start_check()
    if res_data and res_data.get('code') == '0':
        print(res_data.get('msg', ''))
    else:
        try:
            print("license授权成功, 开始执行业务流程")
            if test_time('2025-11-30 16:00:00'):
                app_gui = MainWin(MainUIController())
                init_service()
                # style = ttkbootstrap.Style(theme='vapor')
                # style = ttkbootstrap.Style(theme='cyborg')
                # style = ttkbootstrap.Style(theme='superhero')
                # style = ttkbootstrap.Style(theme='morph')
                # style = ttkbootstrap.Style(theme='morph')
                # style = ttkbootstrap.Style(theme='journal')
                # style = ttkbootstrap.Style(theme='united')
                style = ttkbootstrap.Style(theme='simplex')
                app_gui.mainloop()
            else:
                window = tk.Tk()  # 父容器
                window.title("抖音获客关注私信平台")  # 父容器标题
                window.wm_attributes('-topmost', 1)
                sw = window.winfo_screenwidth()
                sh = window.winfo_screenheight()
                ww = 400
                wh = 300
                x = (sw - ww) / 3
                y = (sh - wh) / 3
                window.geometry('%dx%d+%d+%d' % (ww, wh, x, y))  # 父容器大小
                Dev = tk.LabelFrame(window, text="授权超时", padx=10, pady=5)  # 水平，垂直方向上的边距均为 10
                Dev.place(x=50, y=50)
                text = " 你已经超出授权使用期限\n" \
                       " 请联系管理员进行提权\n         \n" \
                       " 联系：18310722053\n"
                tk.Label(Dev, text=text, justify='left').grid(column=0, row=0, sticky='w', pady=5, padx=5)  # 添加用户账号
                window.mainloop()
        except Exception as e:
            print(e)
